package cn.hxzy.user.mapper;

import cn.hxzy.common.domain.School;
import cn.hxzy.common.domain.User;
import cn.hxzy.common.domain.UserClass;
import cn.hxzy.common.domain.UserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 项目名: education-platform
 * 当前用户: 18735
 * 创建日期: 2024/6/22 下午2:44
 * 项目介绍:
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 登录
     * @param userName 账号
     * @param userPass 密碼
     * @return jwt
     */
    @Select("select * from sys_user where user_name = #{userName} and user_pass = #{userPass}")
    User login(@Param("userName") String userName, @Param("userPass") String userPass);

    @Select("select sys_user.*,sys_teacher_major.major_id,sys_teacher_major.teachering_research_id\n" +
            "from sys_user\n" +
            "left join sys_teacher_major on(sys_user.user_id = sys_teacher_major.teacher_id)\n" +
            "where sys_user.user_id =#{uid}")
    User GetUserinfo(@Param("uid") Long uid);

    /**
     * 修改用户信息
     * @param user 用戶信息
     * @return 1 成功修改
     */
    int updateUser(User user);

    /**
     * 根据id查询用户信息
     * @return 用户信息
     */
    User selectById();


    /**
     * 根据密码查询用户信息
     * @param userPass 密码
     * @return 当前用户
     */
    @Select("select user_pass from sys_user where user_id=#{userId} and user_pass=#{userPass}")
    String selectOldPwd(@Param("userId") Long userId,@Param("userPass") String userPass);


    /**
     * 修改头像
     * @param userAvatar 头像
     * @return 1 修改成功
     */
    int updatePic(@Param("userAvatar") String userAvatar);

    /**
     * 新增学校
     * @param school
     * @return 1 新增成功
     */
    int save(School school);

    @Select("select user_id from sys_user where user_phone = #{phone}")
    Long loginByCode(String phone);

    /**
     * 根据编号查询班级信息
     * @return 返回用户所对应的所有班级信息
     */
    @Select("select * from sys_user_class where user_id = #{userId}")
    List<UserClass> selectClassById(Long userId);


    @Select("SELECT user_id \n" +
            "FROM sys_user\n" +
            "WHERE nick_name =#{userName}")
    List<Integer> getUserIdByUserName(String userName);

    //批量导入学生
    @Insert("INSERT INTO sys_user (user_name,user_pass,user_card,school_id,\n" +
            "\t user_email,user_phone,user_sex,user_type,nick_name)\n" +
            "\t VALUES(#{userName},#{userPass},#{userCard},#{schoolId}," +
            "#{userEmail},#{userPhone},#{userSex},'2',#{nickName})")
    @SelectKey(keyColumn = "user_id",keyProperty = "userId",before = false,resultType =Long.class,statement = {" select last_insert_id()"})
    Integer AddUserXX(User user);

    //信息，配合上面使用
    @Insert("INSERT INTO user_info (user_id,direction_id,add_time,\n" +
            "\t\tstudent_record,user_address,user_postscript)\n" +
            "\tVALUES (#{userId},#{directionId},#{addTime},#{studentRecord},#{userAddress},#{userPostscript})")
    Integer addUserInfo(UserInfo userInfo);

    //添加到班级，
    @Insert("insert into sys_user_class (user_id,class_id) values (#{userId},#{classId})")
    Integer addUserClass(Integer userId,Integer classId);

    @Select("SELECT * FROM sys_user WHERE user_type=2 and  user_id IN (SELECT user_id FROM sys_user_class WHERE class_id=#{classId})")
    List<User> selectUserByClassId(Integer classId);
    @Delete("DELETE FROM sys_user_class WHERE user_id =#{userId}")
    Integer deleteUser(Integer userId);

    @Delete("DELETE FROM sys_user WHERE user_id =#{userId}")
    Integer deleteUserTable(Integer userId);

    @Select("SELECT count(1) FROM sys_user WHERE user_type=2 and  user_id IN (SELECT user_id FROM sys_user_class WHERE class_id=#{classId})")
    Integer selectUserCount(Integer classId);


}
